package com.xff.dao;


import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * FastDFS�����ࡾʵ���ļ��ϴ������ء�ɾ������ѯ��
 * @author 
 */
public class FastDFSClient {

    private  TrackerClient trackerClient = null;
    private  TrackerServer trackerServer = null;
    private  StorageServer storageServer = null;
    private  StorageClient1 storageClient = null;

    public FastDFSClient() throws Exception {
    	ClientGlobal.init(new ClassPathResource("client.conf").getFile().getAbsolutePath());
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
        storageServer = null;
        storageClient = new StorageClient1(trackerServer, storageServer);
    }

    /**
     * �ϴ��ļ�����
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     * @param fileName �ļ�ȫ·��
     * @param extName �ļ���չ������������.��
     * @param metas �ļ���չ��Ϣ
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, String extName, NameValuePair[] metas) {
        String result=null;
        try {
            result = storageClient.upload_file1(fileName, extName, metas);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * �ϴ��ļ�,��fileName
     * @param fileName �ļ��Ĵ���·������ �磺D:/image/aaa.jpg
     * @return nullΪʧ��
     */
    public String uploadFile(String fileName) {
        return uploadFile(fileName, null, null);
    }
    /**
     *
     * @param fileName �ļ��Ĵ���·������ �磺D:/image/aaa.jpg
     * @param extName �ļ�����չ�� �� txt jpg��
     * @return nullΪʧ��
     */
    public  String uploadFile(String fileName, String extName) {
        return uploadFile(fileName, extName, null);
    }

    /**
     * �ϴ��ļ�����
     * <p>Title: uploadFile</p>
     * <p>Description: </p>
     * @param fileContent �ļ������ݣ��ֽ�����
     * @param extName �ļ���չ��
     * @param metas �ļ���չ��Ϣ
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {
        String result=null;
        try {
            result = storageClient.upload_file1(fileContent, extName, metas);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * �ϴ��ļ�
     * @param fileContent �ļ����ֽ�����
     * @return nullΪʧ��
     * @throws Exception
     */
    public String uploadFile(byte[] fileContent) throws Exception {
        return uploadFile(fileContent, null, null);
    }

    /**
     * �ϴ��ļ�
     * @param fileContent  �ļ����ֽ�����
     * @param extName  �ļ�����չ�� �� txt  jpg png ��
     * @return nullΪʧ��
     */
    public String uploadFile(byte[] fileContent, String extName) {
        return uploadFile(fileContent, extName, null);
    }

    /**
     * �ļ����ص�����
     * @param path ͼƬ·��
     * @param output ����� �а���Ҫ��������̵�·��
     * @return -1ʧ��,0�ɹ�
     */
    public int download_file(String path,FileOutputStream output) {
        int result=-1;
        try {
            byte[] b = storageClient.download_file1(path);
            try{
                if(b != null){
                    output.write(b);
                    result=0;
                }
            }catch (Exception e){} //�û�����ȡ��������
            finally {
                if (output != null){
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * ��ȡ�ļ�����
     * @param path �ļ���·�� ��group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return
     */
    public byte[] download_bytes(String path) {
        byte[] b=null;
        try {
            b = storageClient.download_file1(path);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * ɾ���ļ�
     * @param group ���� �磺group1
     * @param storagePath ����������·������ �磺M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return -1ʧ��,0�ɹ�
     */
    public Integer delete_file(String group ,String storagePath){
        int result=-1;
        try {
            result = storageClient.delete_file(group, storagePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return  result;
    }
    /**
     *
     * @param storagePath  �ļ���ȫ��·�� �磺group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return -1ʧ��,0�ɹ�
     * @throws IOException
     * @throws Exception
     */
    public Integer delete_file(String storagePath){
        int result=-1;
        try {
        	 result = ((StorageClient1) storageClient).delete_file1(storagePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return  result;
    }

    /**
     * ��ȡԶ�̷������ļ���Դ��Ϣ
     * @param groupName   �ļ����� �磺group1
     * @param remoteFileName M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
     * @return
     */
    public FileInfo getFile(String groupName,String remoteFileName){
        try {
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}